package com.junchenrobot.mes.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.junchenrobot.mes.domain.InputTask;
import com.junchenrobot.mes.domain.WinderTask;
import com.junchenrobot.mes.dto.AgvWorkInfoDto;
import com.junchenrobot.mes.mapper.WinderTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务Dao
 *
 * @author SeaWave
 * @date 2022/11/28
 */
@Component
public class WinderTaskDao {

    private final WinderTaskMapper winderTaskMapper;

    @Autowired
    public WinderTaskDao(WinderTaskMapper winderTaskMapper) {
        this.winderTaskMapper = winderTaskMapper;
    }

    /**
     * 插入新任务
     *
     * @param winderTask 络筒机任务
     * @return int
     */
    public int insertNewTask(WinderTask winderTask) {
        return winderTaskMapper.insert(winderTask);
    }

    /**
     * 获取任务
     *
     * @param taskCode 任务代码
     * @return {@link InputTask}
     */
    public WinderTask getTask(String taskCode) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).eq(WinderTask::getTaskCode, taskCode).last("LIMIT 1").one();
    }

    /**
     * 获得小车作业信息
     * 包括 作业id 产线号 纺位号 流水号
     *
     * @param taskCode 任务代码
     * @return {@link AgvWorkInfoDto}
     */
    public AgvWorkInfoDto getAgvWorkInfo(String taskCode) {
        return winderTaskMapper.getAgvWorkInfo(taskCode);
    }

    /**
     * 更新任务执行状态
     *
     * @param taskCode 任务编号
     * @param state    状态
     */
    public void updateTaskByTaskCode(String taskCode, Integer state) {
        LambdaUpdateWrapper<WinderTask> uw = new LambdaUpdateWrapper<WinderTask>().set(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskCode, taskCode);
        winderTaskMapper.update(null, uw);
    }
    /**
     * 根据任务id更新任务
     *
     * @param winderTask 络筒机任务
     */
    public void updateTaskById(WinderTask winderTask) {
        winderTaskMapper.updateById(winderTask);
    }

    /**
     * 修改结束时间
     * @param localDateTime
     * @param taskCode
     */
    public void updateFinishedTime(LocalDateTime localDateTime,String taskCode) {

        LambdaUpdateWrapper<WinderTask> wrapper = new LambdaUpdateWrapper<WinderTask>()
                .set(WinderTask::getTaskFinishedTime,localDateTime)
                .eq(WinderTask::getTaskCode, taskCode);
         winderTaskMapper.update(null,wrapper);
    }

    /**
     * 查询设备最新的装载任务
     *
     * @param deviceName 设备名称
     * @return {@link WinderTask}
     */
    public WinderTask getLastInputTask(String deviceName, Integer state) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper)
                .eq(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .eq(WinderTask::getTaskType, 1)
                .last(true, "LIMIT 1")
                .one();
    }

    /**
     * 查询设备最新的放料任务
     *
     * @param deviceName 设备名称
     * @return {@link WinderTask}
     */
    public List<WinderTask> getLastOutputTask(String deviceName, Integer state) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).le(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .eq(WinderTask::getTaskType, 2)
                .last(true, "LIMIT 1")
                .list();
    }

    /**
     * 查询设备最新的取料任务
     *
     * @param deviceName 设备名称
     * @param state      任务执行状态
     * @return
     */
    public List<WinderTask> setLastOutputTask(String deviceName, Integer state) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).le(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .eq(WinderTask::getTaskType, 1)
                .last(true, "LIMIT 1")
                .list();
    }

    /**
     * 查询24小时前到现在的总数
     * @return
     */
    public Long selectCount(){
        Long count = winderTaskMapper.selectCount(new QueryWrapper<WinderTask>()
                .gt("data_create_time", LocalDateTime.now().plusDays(-1)));
        return count;
    }
    /**
     * 查询该小车所有未完成的任务
     * 执行状态小于等于2的为未完成任务
     *
     * @return {@link List}<{@link WinderTask}>
     */
    public List<WinderTask> listUnFinishedNameTask(String deviceName) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).le(WinderTask::getTaskExecuteStatus, 2)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .list();
    }
    /**
     * 查询所有未完成的任务
     * 依据时间倒叙排序
     * 执行状态小于等于2的为未完成任务
     *
     * @return {@link List}<{@link WinderTask}>
     */
    public List<WinderTask> listUnFinishedTask() {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).le(WinderTask::getTaskExecuteStatus, 2)
                .orderByAsc(WinderTask::getTaskDeviceName)
                .orderByAsc(WinderTask::getTaskPoleCode)
                .list();
    }

    /**
     * 查询24小时前到现在的历史记录
     * @return
     */
    public List<WinderTask> ListAll(Integer page,Integer num) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper)
                .eq(WinderTask::getDeleteFlag, 0)
                .gt(WinderTask::getDataCreateTime, LocalDateTime.now().plusDays(-1))
                .orderByDesc(WinderTask::getTaskStartTime)
                .last("LIMIT "+page+","+num)
                .list();
    }


    /**
     * 任务列表状态
     *
     * @param deviceName 设备名称
     * @param state      状态
     * @return {@link List}<{@link WinderTask}>
     */
    public List<WinderTask> listTaskCondition(String deviceName, Integer state, Integer type) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).eq(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .eq(WinderTask::getTaskType, type)
                .last("LIMIT 2")
                .list();
    }
    /**
     * 任务列表状态
     *
     * @param deviceName 设备名称
     * @param state      状态
     * @return {@link List}<{@link WinderTask}>
     */
    public List<WinderTask> listTaskConditionOne(String deviceName, Integer state, Integer type) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper)
                .lt(WinderTask::getTaskExecuteStatus, state)
                .eq(WinderTask::getTaskDeviceName, deviceName)
                .eq(WinderTask::getTaskType, type)
                .last("LIMIT 2")
                .list();
    }

    /**
     * 列出已完成任务
     * 执行状态大于等于2的为完成的任务
     *
     * @return {@link List}<{@link WinderTask}>
     */
    public List<WinderTask> listFinishedTask() {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).ge(WinderTask::getTaskExecuteStatus, 2)
                .orderByDesc(WinderTask::getDataCreateTime)
                .list();
    }


    /**
     * 根据任务id查询任务
     *
     * @param taskId 任务id
     * @return {@link WinderTask}
     */
    public WinderTask getWinderTask(Long taskId) {
        return new LambdaQueryChainWrapper<>(winderTaskMapper).eq(WinderTask::getId, taskId).last(true, "LIMIT 1").one();
    }

    /**
     * 删除任务
     *
     * @param winderTask 任务
     */
    public void deleteWinderTask(WinderTask winderTask) {
        winderTaskMapper.deleteById(winderTask);
    }

    public void deleteWinderTask(String taskCode) {
        QueryWrapper<WinderTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_code", taskCode);
        winderTaskMapper.delete(queryWrapper);
    }


}
